﻿using System.Collections.Immutable;
using System.Data;
using System.Linq;
using Vabp.Permissions;
using Vabp.Roles;
using Vabp.Users;
using Microsoft.EntityFrameworkCore;
using Volo.Abp;
using Volo.Abp.EntityFrameworkCore.Modeling;

namespace abp.EntityFrameworkCore
{
    public static class VabpDbContextModelCreatingExtensions
    {
        public static void Configureabp(this ModelBuilder builder)
        {
            Check.NotNull(builder, nameof(builder));

            /* Configure your own tables/entities inside here */
            builder.Entity<MyTest>(b =>
            {
                b.Property<decimal>(p => p.Quantity).HasColumnType("decimal(18,3)");
                b.ToTable("MyTest");
            });

            builder.Entity<LoginPermissionGranted>(p =>
            {
                p.ToTable("LoginPermissionGranteds");
                p.ConfigureByConvention();
            });

            builder.Entity<LoginUserRole>(ur =>
            {
                ur.ConfigureByConvention();
                ur.ToTable("LoginUserRoles");
                ur.HasKey(a => new {a.UserId, a.RoleId});
            });

            builder.Entity<LoginUser>(u =>
            {
                u.ToTable("LoginUsers");
                u.ConfigureByConvention();
            });
            builder.Entity<LoginRole>(r =>
            {
                r.ToTable("LoginRoles");
                r.ConfigureByConvention();
            });
            builder.Entity<LoginUserRole>().HasOne(ur => ur.User)
                .WithMany(ur => ur.UserRoles)
                .HasForeignKey(ur => ur.UserId);
            builder.Entity<LoginUserRole>().HasOne(ur => ur.Role)
                .WithMany(r => r.UserRoles)
                .HasForeignKey(ur => ur.UserId);

        }
    }
}